/*
 * VistaTarifas.java
 *
 * Created on 23 de noviembre de 2008, 08:19 PM
 * Author : Julio Nevado
 */

package ddta;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JDialog; 
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author  julio
 */
public class VistaTarifas extends JDialog {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.7A49617A-F96F-2D88-6B50-8967D6C388FC]
    // </editor-fold> 
    private DDTA ddta;
    private DefaultTableModel tableModel;

    /** Creates new form VistaTarifas */
    public VistaTarifas(JDialog parent, boolean modal, DDTA ddta) {
        super(parent, modal);
        initComponents();
        this.ddta = ddta;
        this.tableModel = (DefaultTableModel) tblTarifas.getModel();
        tblTarifas.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        mostrarTarifa();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCodTarifa = new javax.swing.JTextField();
        txtPNinio = new javax.swing.JTextField();
        txtPTerceraEdad = new javax.swing.JTextField();
        txtPAdulto = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTarifas = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tarifas de Tours");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tarifas de tours"));

        jLabel1.setText("Precio Niño: ");

        jLabel2.setText("Código de Tarifa :");

        jLabel3.setText("Precio tercera edad :");

        jLabel4.setText("Precio Adulto:");

        btnGuardar.setText("Guardar tarifa");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txtPNinio, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(50, 50, 50)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtCodTarifa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPAdulto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPTerceraEdad, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addGap(34, 34, 34))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPNinio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPAdulto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPTerceraEdad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de tarifas"));

        tblTarifas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código de tarifa", "P.Niño", "P.Adulto", "P. Tercera Edad"
            }
        ));
        jScrollPane1.setViewportView(tblTarifas);

        btnEliminar.setText("Eliminar seleccionado");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnEliminar)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(8, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEliminar)
                .addContainerGap())
        );

        btnCerrar.setText("  Cerrar  ");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCerrar)
                        .addGap(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
// TODO add your handling code here:
    String codigoTarifa = txtCodTarifa.getText();
    double precNinio = 0, precPadulto = 0, precTerc = 0;
    if (codigoTarifa.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese código de tarifa", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        txtCodTarifa.requestFocusInWindow();
        return;
    }
    try {
        precNinio = Double.parseDouble(txtPNinio.getText());
    } catch(Exception e) {
        JOptionPane.showMessageDialog(this, "Precio de Niño inválido", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        txtPNinio.setText("");
        return;
    }
    try {
        precPadulto = Double.parseDouble(txtPAdulto.getText());
    } catch(Exception e) {
        JOptionPane.showMessageDialog(this, "Precio adulto inválido",e.getMessage(), JOptionPane.ERROR_MESSAGE);
        txtPAdulto.setText("");
        return;
    }
    try {
        precTerc = Double.parseDouble(txtPTerceraEdad.getText());
    } catch(Exception e) {
        JOptionPane.showMessageDialog(this, "Precio Tercera edad inválido", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        txtPTerceraEdad.setText("");
        return;
    }
    if (precNinio > 0 && precPadulto >0 && precTerc > 0) {
        Tarifa tarifa = this.ddta.getCtlTarifas().buscarTarifa(codigoTarifa);
        if (tarifa == null) {
           this.ddta.getCtlTarifas().registrarTarifa(codigoTarifa, 
               precNinio, 
               precPadulto,
               precTerc);
           mostrarTarifa();
           txtCodTarifa.setText("");
           txtPAdulto.setText("");
           txtPNinio.setText("");
           txtPTerceraEdad.setText("");
           txtCodTarifa.requestFocusInWindow();
        } else {
            JOptionPane.showMessageDialog(this, "Código de tarifa ya existe", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    } else {
        JOptionPane.showMessageDialog(this, "Debe ingresar datos válidos", this.getTitle(), JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnGuardarActionPerformed

private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
    if (this.getParent() != null) {
        ((VistaPaquetes)this.getParent()).rellenarComboTarifas();
        this.setVisible(false);
    }
}//GEN-LAST:event_btnCerrarActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
    int iRow = this.tblTarifas.getSelectedRow();
    if (iRow >= 0) {
        String codTarifa = this.tableModel.getValueAt(iRow, 0).toString();
        this.ddta.getCtlTarifas().eliminarTarifa(codTarifa);
        mostrarTarifa();
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar un item a eliminar", this.getTitle(), JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    if (this.getParent() != null) {
        ((VistaPaquetes)this.getParent()).rellenarComboTarifas();
        this.setVisible(false);
    }
}//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTarifas;
    private javax.swing.JTextField txtCodTarifa;
    private javax.swing.JTextField txtPAdulto;
    private javax.swing.JTextField txtPNinio;
    private javax.swing.JTextField txtPTerceraEdad;
    // End of variables declaration//GEN-END:variables

private void mostrarTarifa() {
    int iRows = this.ddta.getCtlTarifas().getTarifas().size();
    if (iRows > 0) {
        this.tableModel.setNumRows(iRows);
        int i = 0;
        for (Tarifa tar : this.ddta.getCtlTarifas().getTarifas()) {
            this.tableModel.setValueAt(tar.getCodigoTarifa(), i, 0);
            this.tableModel.setValueAt(tar.getPrecioNinio(), i, 1);
            this.tableModel.setValueAt(tar.getPrecioAdulto(), i, 2);
            this.tableModel.setValueAt(tar.getPrecioAdultomayor(), i, 3);
            i++;
        }
    }
}

}
